//package com.wwj.my_ummary.util.email;
//
//import com.wwj.my_ummary.util.numberUtils.StrUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.mail.SimpleMailMessage;
//import org.springframework.mail.javamail.JavaMailSenderImpl;
//import org.springframework.mail.javamail.MimeMessageHelper;
//
//import javax.mail.*;
//import javax.mail.internet.InternetAddress;
//import javax.mail.internet.MimeMessage;
//import java.security.Security;
//import java.util.Date;
//import java.util.Properties;
//
///**
// * 发送邮件工具类
// */
//public class EmailUtil {
//
//    public static final Logger LOGGER = LoggerFactory.getLogger(EmailUtil.class);
//
//    private static final String EMAIL_SMTP;
//    private static final String EMAIL_FROM;
//    private static final String EMAIL_PASS;
//    private static final String EMAIL_FROMNAME;
//    private static final int EMAIL_PORT;
//
////    static {
////        EMAIL_SMTP = SysHelper.getConfig(GlobalConstant.EMAIL_SMTP);
////        EMAIL_FROM = SysHelper.getConfig(GlobalConstant.EMAIL_FROM);
////        EMAIL_PASS = SysHelper.getConfig(GlobalConstant.EMAIL_PASS);
////        EMAIL_FROMNAME = SysHelper.getConfig(GlobalConstant.EMAIL_FROMNAME);
////        EMAIL_PORT = SysHelper.getIntConfig(GlobalConstant.EMAIL_PORT);
////    }
//
//    static {
//        EMAIL_SMTP = "smtp.163.com";
//        EMAIL_FROM = "rhkj365@163.com";
//        EMAIL_PASS = "chenxr1101";
//        EMAIL_FROMNAME = "火蚁矿业";
//        EMAIL_PORT = 465;
//    }
//
////    static {
////    	EMAIL_SMTP = "smtp.qq.com";
////    	EMAIL_FROM = "364421014@qq.com";
////    	EMAIL_PASS = "hlhvctfzwfqubicf";
////    	EMAIL_FROMNAME = "Hello~! 风神";
////    	EMAIL_PORT = 465;
////    }
//
//    /**
//     * 发送邮件
//     *
//     * @param address
//     * @param title
//     * @param content
//     * @return
//     */
//    public static Boolean doSend(String address, String title, String content) {
//        Boolean flag = false;
//        if (465 == EMAIL_PORT) {
//            flag = sslSend(address, title, content);
//        } else {
//            flag = doSendMessage(address, title, content);
//        }
//        return flag;
//    }
//
//    /**
//     * 发送邮件
//     *
//     * @param address
//     * @param title
//     * @param content
//     * @return
//     */
//    public static Boolean doSendMessage(String address, String title, String content) {
//        try {
//            LOGGER.info("doSendMessage发送邮件，地址={}", address);
//            if (StrUtils.isEmail(address)) {
//                JavaMailSenderImpl tmpl = new JavaMailSenderImpl();
//                tmpl.setHost(EMAIL_SMTP);
//                tmpl.setUsername(EMAIL_FROM);
//                tmpl.setPassword(EMAIL_PASS);
//                tmpl.setPort(EMAIL_PORT);
//
//                Properties properties = new Properties();
//                tmpl.setJavaMailProperties(properties);
//
//                SimpleMailMessage message = new SimpleMailMessage();
//                message.setFrom(EMAIL_FROM);
//                message.setTo(address);
//                message.setSubject(title);
//                message.setText(content);
//
//                MimeMessage mimeMsg = tmpl.createMimeMessage();
//                MimeMessageHelper helper = new MimeMessageHelper(mimeMsg, true, "utf-8");
//                helper.setTo(message.getTo());
//                helper.setSubject(message.getSubject());
//                helper.setFrom(message.getFrom(), EMAIL_FROMNAME);
//                helper.setText(message.getText(), true);
//                tmpl.send(mimeMsg);
//                LOGGER.info("邮件{}发送成功", address);
//            }
//            return true;
//        } catch (Exception e) {
//            LOGGER.info("邮件{}发送失败={}", address, e.getMessage());
//            return false;
//        }
//    }
//
//    /**
//     * SSL发送邮件
//     *
//     * @param address
//     * @param title
//     * @param content
//     * @return
//     */
//    public static boolean sslSend(String address, String title, String content) {
//        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
//        // Get a Properties object
//        Properties props = new Properties();
//        props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
//        props.setProperty("mail.smtp.socketFactory.fallback", "false");
//        props.setProperty("mail.smtp.port", String.valueOf(EMAIL_PORT));
//        props.setProperty("mail.smtp.socketFactory.port", String.valueOf(EMAIL_PORT));
//        props.setProperty("mail.smtp.host", EMAIL_SMTP);
//        props.put("mail.smtp.auth", "true");
//        Session session = Session.getDefaultInstance(props, new Authenticator() {
//            protected PasswordAuthentication getPasswordAuthentication() {
//                return new PasswordAuthentication(EMAIL_FROM, EMAIL_PASS);
//            }
//        });
//        Message msg = new MimeMessage(session);
//
//        try {
//            // 设置发件人和收件人
//            msg.setFrom(new InternetAddress(EMAIL_FROM));
//            Address to[] = new InternetAddress[1];
//            to[0] = new InternetAddress(address);
//            // 多个收件人地址
//            msg.setRecipients(Message.RecipientType.TO, to);
//            msg.setSubject(title); // 标题
//            msg.setContent(content, "text/html;charset=utf-8");
//            msg.setSentDate(new Date());
//            Transport.send(msg);
//            LOGGER.info("EmailUtil ssl协议邮件发送成功={}", address);
//            return true;
//        } catch (MessagingException e) {
//            LOGGER.info("sslSend邮件{}发送失败={}", address, e.getMessage());
//            return false;
//        }
//    }
//
//    public static void main(String[] args) {
//        String address = "364421014@qq.com";
//        String title = "邮件发送测试";
//        String content = "测试验证码:123456。";
//        doSend(address, title, content);
//    }
//}
